#ifndef VEDIO_DEVICE_H
#define VEDIO_DEVICE_H
#include <opencv2/opencv.hpp>
#include "my_stitcher.h"
class vedioDevice{
public:
    vedioDevice(void){
            // R相机参数
        dist_r_ = (cv::Mat_<double>(1, 5) << -0.215574313119506, 0.0503982657031021, -0.000326006012063509, 0.000522571068705912, -0.00582027300557630);
        mtx_r_ = (cv::Mat_<double>(3, 3) << 737.117865997755, 0, 874.494661526745, 0, 741.550615514470, 493.947942423772, 0, 0, 1);
        cv::Mat mtx_r_transposed = mtx_r_.t();

        // L相机参数
        dist_l_ = (cv::Mat_<double>(1, 5) << -0.213745254672620, 0.0478679666952374, 0.000420728066086759, 0.000609686043916338, -0.00514436238632554);
        mtx_l_ = (cv::Mat_<double>(3, 3) << 733.209634145040, 0, 991.941096721751,
                         0, 745.645448636599, 543.182418691498,
                         0, 0, 1);
        cv::Mat mtx_l_transposed = mtx_l_.t();

        // 打开第一个相机
        capL_.open(0);
        capL_.set(cv::CAP_PROP_FRAME_WIDTH, 1920);
        capL_.set(cv::CAP_PROP_FRAME_HEIGHT, 1080);

        // 打开第二个相机
        capR_.open(1);
        capR_.set(cv::CAP_PROP_FRAME_WIDTH, 1920);
        capR_.set(cv::CAP_PROP_FRAME_HEIGHT, 1080);

        capR_ >> frameR_;
        capL_ >> frameL_;

        // 读取图像
        cv::Mat image1 = cv::imread("cameraL_001.jpg");
        cv::Mat image2 = cv::imread("cameraR_001.jpg");

        // 检查图像是否成功读取
        if (image1.empty() || image2.empty())
        {
            std::cerr << "Error: Unable to read images." << std::endl;
        }

        // 将图像放入一个向量中
        std::vector<cv::Mat> images1;
        images1.push_back(image1);
        images1.push_back(image2);


        // 创建拼接器
        stitcher_ = cv::Stitcher::create();

        // 进行拼接
        cv::Mat result1;
        cv::Stitcher::Status status = stitcher_->stitch(images1, result1);

        // 检查拼接是否成功
        if (status != cv::Stitcher::OK)
        {
            std::cerr << "Error: Stitching failed. Status code: " << status << std::endl;
        }

        // 保存拼接结果
        cv::imwrite("result.jpg", result1);
    }
    void getFrame(cv::Mat &frame){
        
        capR_ >> frameR_;
        capL_ >> frameL_;

        // 校正图像
        
        cv::undistort(frameR_, frameR_undistorted_, mtx_r_, dist_r_);
        cv::undistort(frameL_, frameL_undistorted_, mtx_l_, dist_l_);

        // 进行拼接
        images_.clear();
        images_.push_back(frameR_undistorted_);
        images_.push_back(frameL_undistorted_);
        
        cv::Stitcher::Status status = stitcher_->stitch(images_, frame);
        cv::resize(frame, frame, cv::Size(800, 600));
    }
    ~vedioDevice(){
        // 释放摄像头资源
        capL_.release();
        capR_.release();
    }
private:
    cv::VideoCapture capL_;
    cv::VideoCapture capR_;
    cv::Mat frameR_;
    cv::Mat frameL_;
    cv::Mat frameR_undistorted_;
    cv::Mat frameL_undistorted_;
    cv::Mat dist_r_;
    cv::Mat mtx_r_;

    cv::Mat dist_l_;
    cv::Mat mtx_l_;
    std::vector<cv::Mat> images_;
    cv::Ptr<cv::Stitcher> stitcher_;
};
#endif
